package com.syncnews.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.syncnews.entity.VietBaoEntity;

public class BienAp extends BaseDAOBienAp {

	public boolean onSelect(String newsSeo) throws Exception {
		boolean check = false;
		Connection conn = getConnection();
		PreparedStatement pStmt = conn.prepareStatement("SELECT * FROM News WHERE NewsSEOUrl = ?");
		
		pStmt.setString(1, newsSeo.trim());
		ResultSet rs = pStmt.executeQuery();
		if(rs.next()) {
			check = true;
		} 
		rs.close();
		pStmt.close();
		conn.close();
		return check;

	}
	public void onInsert(int category,String title,String content,String image,String desc,String metaTitle,String metaDesc,String metaKey,String newsSeo) throws Exception {
		if(onSelect(newsSeo)) {
			System.out.println("Tin co roi");
		} else {
			System.out.println(title);
			Connection conn = getConnection();
			PreparedStatement pStmt = conn.prepareStatement("INSERT INTO [News]([CategoryId],[NewsTitle],[NewsContent],[NewsImages],[NewsDescription],[NewsMetaTitle],[NewsMetaDescription],[NewsMetaKeyword],[NewsSEOUrl]) VALUES (?,?,?,?,?,?,?,?,?)");
			pStmt.setInt(1, category);
			pStmt.setString(2, title);
			pStmt.setString(3, content);
			pStmt.setString(4, image);
			pStmt.setString(5, desc);
			pStmt.setString(6, metaTitle);
			pStmt.setString(7, metaDesc);
			pStmt.setString(8, metaKey);
			pStmt.setString(9, newsSeo.trim());
			int result = pStmt.executeUpdate();
			System.out.println(result);
			pStmt.close();
			conn.close();
		}
	}
	public static void main(String[] args) {
		try {
			VietBao24hDAO bao24hDAO = new VietBao24hDAO();
			BienAp ap = new BienAp();
			ArrayList<VietBaoEntity> list = bao24hDAO.selectNews(2,500);
			for(VietBaoEntity baoEntity : list) {
				
				ap.onInsert(14, baoEntity.getTitle(), baoEntity.getDescription(), baoEntity.getImage(), baoEntity.getContent(), 
						baoEntity.getTitle(), baoEntity.getDescription(), "", baoEntity.getSeoUrl());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		

	}

}
